{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "path='/home/zhongy/test/main.cpp'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "该路径保存有一个main.cpp文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def analyzeCode(filePath):\n",
    "    totalLines=0\n",
    "    commentLine=0\n",
    "    blankLine=0\n",
    "\n",
    "    with open(filePath,encoding='gb18030',errors='ignore') as f:\n",
    "        lines=f.readlines()\n",
    "        totalLines=len(lines)\n",
    "        lineIndex=0\n",
    "        while lineIndex<totalLines:\n",
    "            line=lines[lineIndex]\n",
    "            if line.startswith(\"//\"):\n",
    "                commentLine+=1\n",
    "                lineIndex+=1\n",
    "            elif line=='\\n':\n",
    "                blankLine+=1\n",
    "                lineIndex+=1\n",
    "            else:\n",
    "                lineIndex+=1\n",
    "    print(\"代码总行数是： \",totalLines)\n",
    "    print(\"注释总行数是： \",commentLine)\n",
    "    print(\"空行总行数是： \", blankLine)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义了一个分析函数，默认检测的是c标准注释，以//开头。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "代码总行数是：  13\n",
      "注释总行数是：  2\n",
      "空行总行数是：  2\n"
     ]
    }
   ],
   "source": [
    "analyzeCode(path)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
